<!DOCTYPE html>
<html lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
	<meta name="description" content="">
	<meta name="author" content="">
	<!-- STYLESHEETS -->
	<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
	<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
	<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
	<link rel="stylesheet" type="text/css" href="../../css/responsive.css">
	<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
	<!-- file-input -->
	<link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />
	<!-- SELECT2 -->
	<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
	<!-- datatable -->
	<link rel="stylesheet" href="../../js/datatables/datatable.css">
	<link rel="stylesheet" href="../../js/datepicker/datepicker.css">
	<!-- JQUERY -->
	<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
	<script type="text/javascript" src="../../js/datepicker/datepicker.js"></script>
	<script src="../../js/common.js"></script>
	<!-- FONTS 
  <link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
  -->
	<style>
		.dataTables_scrollHeadInner table th {
      text-align: center;
    }
    .dataTables_scrollBody table td {
      text-align: center;
    }
    .dataTables_scrollBody table td .btn + .btn {
      margin-left: 8px;
    }
    /* .dataTables_scrollBody table tr td:last-child {
      text-align: left;
    } */
	</style>
</head>

<body>
	<header class="navbar clearfix" id="header">
	</header>

	<!-- PAGE -->
	<section id="page">
		<!-- SIDEBAR -->
		<div id="sidebar" class="sidebar">
			<div class="sidebar-menu nav-collapse">

				<!-- SIDEBAR MENU -->
				<ul>
				</ul>
			</div>


		</div>
		<!-- /SIDEBAR -->
		<div id="main-content">
			<div class="container">
				<div class="row">
					<div id="content" class="col-lg-12">
						<!-- PAGE HEADER-->
						<div class="row">
							<div class="col-sm-12">
								<div class="page-header">
									<!-- STYLER -->

									<!-- /STYLER -->
									<!-- BREADCRUMBS -->
									<ul class="breadcrumb">
										<li>
											<i class="fa fa-home"></i>
											<a href="index.html">首页</a>
										</li>
										<li>
											<a href="#">CMS管理</a>
										</li>
										<li>壹家店首页推荐</li>
									</ul>
									<!-- /BREADCRUMBS -->

								</div>
							</div>
						</div>
						<!-- /FORMS QUERY -->
						<div class="row">
							<!-- TABLES -->
							<div class="col-md-6">
								<!-- BOX -->
								<div class="box border primary">
									<div class="box-title">
										<h4><i class="fa fa-search"></i> 查询商品</h4>
									</div>
									<div class="box-body">
										<div class="form-group mrg-bt10">
											<label class="col-sm-1 control-label" style="width: auto">商品名称：</label>
											<div class="col-sm-5">
												<input type="text" class="form-control" name="q" placeholder="请输入商品名称">
											</div>
											<div class="col-sm-6" style="width: auto">
												<a class="btn btn-primary search"><i class="fa fa-search"></i> 查询</a>
												<a class="btn btn-success add-all"><i class="fa  fa-plus-square-o"></i> 全部加入</a>
											</div>
										</div>
										<table id="db_Pages_list" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">

										</table>
									</div>
								</div>
								<!-- /BOX -->
							</div>
							<div class="col-md-6">
								<div class="box border primary">
									<div class="box-title">
										<h4><i class="fa fa-table"></i> 已选商品</h4>
									</div>
									<div class="box-body">
										<!-- <div class="form-group mrg-bt10">
											<label class="col-sm-1 control-label" style="width: auto">组合名称：</label>
											<div class="col-sm-5">
												<input type="text" class="form-control" name="name" placeholder="请输入组合名称">
											</div>
										</div> -->
										<table id="select_product_list" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">

										</table>
									</div>
								</div>
							</div>

						</div>
						<div style="text-align: center">
							<a href="javascript:;" class="btn btn-success config bt-submit" style="width: 100px;margin-top:20px;"><i class="fa fa-save"></i>
								保存</a>
						</div>
						<!-- /TABLES -->


						<div class="footer-tools">
							<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
						</div>
					</div><!-- /CONTENT-->
				</div>
			</div>
		</div>
	</section>
	<!--/PAGE -->
	<!-- JAVASCRIPTS -->
	<!-- Placed at the end of the document so the pages load faster -->

	<!-- JQUERY UI-->
	<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
	<!-- BOOTSTRAP -->
	<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

	<!-- BLOCK UI -->
	<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>

	<!-- AUTOSIZE -->
	<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>

	<!-- INPUT MASK -->
	<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
	<!-- SELECT2 -->
	<script type="text/javascript" src="../../js/select2/select2.min.js"></script>

	<!-- bootstrap-fileupload -->
	<script type="text/javascript" src="../../js/jquery-upload/js/jquery.iframe-transport.js"></script>
	<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload.min.js"></script>
	<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-process.min.js"></script>
	<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-validate.min.js"></script>

	<!-- UNIFORM -->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
	<!-- spinner -->
	<script src="../../js/spinner/spin.js"></script>
	<!-- DATA TABLES -->
	<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
	<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
	<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
	<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
	<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>

	<!--bootbox-->
	<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
	<!-- COOKIE -->
	<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
	<!-- CUSTOM SCRIPT -->
	<script src="../../js/script.js"></script>
	<script>
		var id = $.getUrlParam("id");
		var selectProductList = []
		var productList = [] // id合集
		var Pages = function () {
			var $mainDataTable = null;
			var $selectProductTable = null;
			var searchProduct = function () {
				var q = $("input[name=q]").val();
				var url = App.getContextPath() + "admin/product/searchProduct.do?q=" + q + '&isSpecial=false&productStatus=OnSale&salesPlatform=Yijiadian';
				if ($mainDataTable) {
					$mainDataTable.api().ajax.url(url).load();
					return;
				}
				var dtSetting = $.dataTableSetting({
					"ajaxSource": url,
					"serverSide": true,
					"bFilter": false, // 去掉搜索框
					"bLengthChange": true, // 每页显示数量							
					"sScrollX": "100%",
					"sScrollXInner": "100%",
					"iDisplayLength": 10,
					"bSort": false,
					"aoColumns": [{
							"mDataProp": "id",
							"sTitle": "商品ID",
							"width": "10%",
							"class": 'productId'
						},
						{
							"mDataProp": "productName",
							"sTitle": "商品名称",
							"width": "50%",
							"class": 'productName'
						}, {

							"sTitle": "颜色",
							"width": "20%",
							"mDataProp": "shortName",
							"class": 'shortName'
						}, {
							"sTitle": "操作",
							"width": "10%",
							"render": function (data, type, row) {
								var str = ''
								var id = row.id + ''
								var index = productList.indexOf(id)
								if (index == -1) {
									str +=
										"<a class='btn btn-success btn-xs btn-width btn-add' style='width:60px;margin-left:0px;' id='" + row.id +
										"'><i class='fa fa-plus-square-o'></i> 加入</a>";
								} else {
									str +=
										"<a class='btn btn-danger btn-xs btn-width btn-add' style='width:60px;margin-left:0px;' id='" + row.id +
										"'><i class='fa fa-trash-o'></i> 取消</a>";
								}
								return str;
							}
						}
					]
				});
				$mainDataTable = $("#db_Pages_list").dataTable(dtSetting);
				if (!$selectProductTable) {
					createSelectProduct(selectProductList)
				}
			};
			var createSelectProduct = function (data) {
				for (var index = 0; index < data.length; index++) {
					var element = data[index];
					if (element.number == undefined) {
						element.number = 0
					}
				}
				var dtSetting = $.dataTableSetting({
					"serverSide": false,
					"bFilter": false, // 去掉搜索框
					"sScrollX": "100%",
					"sScrollXInner": "100%",
					"bSort": false,
					"paging": false,
					"data": data,
					"aoColumns": [{
							"mDataProp": "id",
							"sTitle": "ID",
							"width": "10%",
							"class": 'productId'
						},
						{
							"mDataProp": "productName",
							"sTitle": "商品名称",
							"width": "50%",
							"class": 'productName'
						}, {

							"sTitle": "颜色",
							"width": "20%",
							"mDataProp": "shortName",
							"class": 'shortName'
						}, {
							"sTitle": "序号",
							"width": "14%",
							"mDataProp": "number",
							"class": 'number',
							"render": function (data, type, row) {
								var str = ''
								str +=
									"<input class='numberInput form-control' value='" + row.number +"' type='number' />";
								return str;
							}
						}, {
							"sTitle": "操作",
							"width": "10%",
							"render": function (data, type, row) {
								var str = ''
								str +=
									"<a class='btn btn-danger btn-xs btn-width btn-qx' style='width:60px;margin-left:0px;'><i class='fa fa-trash-o'></i> 取消</a>";
								return str;
							}
						}
					]
				});
				$selectProductTable = $("#select_product_list").dataTable(dtSetting);
			}
			var sreachPairs = function () {
				$.ajax({
					type: "GET",
					url: App.getContextPath() + "admin/hpp/getHomePageProductInfo.do",
					async: true,
					success: function (result) {
						console.log(result)
						var detailList = result.extra.pList || []
						for (var index = 0; index < detailList.length; index++) {
							var item = detailList[index];
							productList.push(item.productId+'')
							selectProductList.push({
								id: item.productId,
								productName: item.productInfo.productName,
								shortName: item.productInfo.color,
								number: item.sortNum
							})
						}
						createSelectProduct(selectProductList)
					}
				});
			}
			return {
				init: function () {
					// searchProduct();
					sreachPairs()
					$('.search').click(searchProduct)
					$('body').on('keyup', '.numberInput', function (e) {
						var index=  $(this).closest('tr').index()
						selectProductList[index].number = $(this).val()
					})
					$("body").on('click', '.btn-add', function () {
						var $this = $(this)
						var $tr = $this.closest('tr')
						var productId = $this.attr('id')
						var productName = $tr.find('.productName').text()
						var shortName = $tr.find('.shortName').text()
						var index = productList.indexOf(productId)
						if (index == -1) {
							selectProductList.push({
								id: productId,
								productName: productName,
								shortName: shortName
							})
							productList.push(productId)
							$this.removeClass('btn-success').addClass('btn-danger').html('<i class="fa fa-trash-o"></i> 取消')
						} else {
							productList.splice(index, 1)
							selectProductList.splice(index, 1)
							$this.removeClass('btn-danger').addClass('btn-success').html('<i class="fa fa-plus-square-o"></i> 加入')
						}
						createSelectProduct(selectProductList)
					})
					// 全部加入
					$("body").on('click', '.add-all', function () {
						$(".btn-add").each(function (index, ele) {
							var $this = $(ele);
							var id = $this.attr('id')
							var index = productList.indexOf(id)
							if (index == -1) {
								var $tr = $this.closest('tr')
								var productName = $tr.find('.productName').text()
								var shortName = $tr.find('.shortName').text()
								selectProductList.push({
									id: id,
									productName: productName,
									shortName: shortName
								})
								productList.push(id)
								$this.removeClass('btn-success').addClass('btn-danger').html('<i class="fa fa-trash-o"></i> 取消')
							}
						})
						createSelectProduct(selectProductList)
					})
					$("body").on('click', '.btn-qx', function () {
						var $this = $(this)
						var $tr = $this.closest('tr');
						var productId = $tr.find('.productId').text()
						// 找到列表中对应商品改变状态
						$("#" + productId).removeClass('btn-danger').addClass('btn-success').html(
							'<i class="fa fa-plus-square-o"></i> 加入')
						var index = productList.indexOf(productId)
						if (index > -1) {
							productList.splice(index, 1)
							selectProductList.splice(index, 1)
							createSelectProduct(selectProductList)
						}
					})
					$('.bt-submit').click(function () {
						// if (!productList.length) {
						// 	$.alert('请先选择商品!')
						// 	return false
						// }
						var productIds = []
						selectProductList.forEach(function(item) {
							productIds.push({
								productId: item.id,
								sortNum: item.number
							})
						})
						$.confirm('确认保存此商品推荐组合?', function () {
							$.ajax({
								type: "POST",
								url: App.getContextPath() + "admin/hpp/setHomePageProduct.do",
								async: true,
								data: JSON.stringify(productIds),
								contentType: 'application/json',
								success: function (result) {
									bootbox.dialog({
										message: "保存商品推荐组合成功!",
										title: "操作提示"
									});
								}
							});
						});
					})
				}
			}
		}();
		$(function () {
			$("#header").load("../head.html");
			App.init(); //Initialise plugins and elements	 
			Pages.init();
		});
	</script>
	<!-- /JAVASCRIPTS -->
</body>

</html>